﻿using Microsoft.Graphics.Canvas;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using 纸片人.AttributeUsageFloder;

// https://go.microsoft.com/fwlink/?LinkId=234238 上介绍了“空白页”项模板

namespace 纸片人.PageViews
{
    [LastModified("2022-5-12", "create HistogramPage.xaml")]
    [PageType("cpu", "直方图")]
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class HistogramPage : Page
    {
        int width = 0, height = 0;

        Color[] source;
        Color[] tempSource;

        int[] RGBHistogram = new int[256];
        int[] RHistogram = new int[256];
        int[] GHistogram = new int[256];
        int[] BHistogram = new int[256];


        public HistogramPage()
        {
            this.InitializeComponent();
        }

        private void findEdge_Click(object sender, RoutedEventArgs e)
        {

        }

        private async void select_Click(object sender, RoutedEventArgs e)
        {
            CanvasBitmap bitmap = await Function.SelectImageAsync();

            Function.InitialParameter(bitmap, out width, out height, out source, out tempSource);
            Function.InitialImage(source, width, height, image);
            RGBHistogram = new int[256];
             RHistogram = new int[256];
             GHistogram = new int[256];
             BHistogram = new int[256];
            ParsingHistogram(source);
            canvas.Invalidate();
        }
        double precentWidth;
        int colorScale = 0;
        private void canvas_Draw(Microsoft.Graphics.Canvas.UI.Xaml.CanvasControl sender, Microsoft.Graphics.Canvas.UI.Xaml.CanvasDrawEventArgs args)
        {
            double canvasWidth =canvas.ActualWidth;
            double canvasHiehgt = canvas.ActualHeight;
            //double max = (double)width * (double)height / 256.0;
            int max = RGBHistogram.Max();
            tb.Text = max+"";
            Debug.WriteLine(max);
            int rMax = RHistogram.Max();
            int gMax = GHistogram.Max();
            int bMax = BHistogram.Max();
             precentWidth = canvasWidth / 256;
            double x = 0;
            using(var ds = args.DrawingSession)
            {
                foreach (int item in RGBHistogram)
                {
                    double rectHeight;


                    if (item == 0)
                    {
                        rectHeight = 0;
                    }
                    else
                    {
                        rectHeight = (double)item / (double)max * canvasHiehgt;

                    }
                    double y = canvasHiehgt - rectHeight;
                    Rect rect = new Rect(x, y, precentWidth, rectHeight);
                    ds.FillRectangle(rect, Colors.Black);
                    x += precentWidth;
                }
            }
        }

        private void canvas_PointerMoved(object sender, PointerRoutedEventArgs e)
        {
            Point point = e.GetCurrentPoint(canvas).Position;

            colorScale = (int)(point.X / precentWidth);

            int count = RGBHistogram[colorScale];

            tb.Text = "色阶:" + colorScale + " 数量:" + count;

        }

        void ParsingHistogram(Color[] sour)
        {
            foreach (Color item in sour)
            {
                RHistogram[item.R]++;
                GHistogram[item.G]++;
                BHistogram[item.B]++;

            }

            for (int i = 0; i < 256; i++)
            {
                RGBHistogram[i] = (RHistogram[i] + GHistogram[i] + BHistogram[i]) ;
            }


        }

    }
}
